package com.haixee.parkdemo.gateway.filter;

import com.haixee.parkdemo.common.response.CommonResEnum;
import com.haixee.parkdemo.common.response.ResResult;
import com.haixee.parkdemo.common.response.ResUtil;
import com.haixee.parkdemo.gateway.utils.RequestUtil;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 404
 * @author GUIFENG GUO
 * 2020/9/11 16:16
 */
@Slf4j
//@Component
public class NotFoundFilter extends AbstractGatewayFilterFactory {


    @Override
    public GatewayFilter apply(Object config) {

        return new InnerFilter();
    }

    @Data
    private static class InnerFilter implements GatewayFilter, Ordered {

        @SneakyThrows
        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            ResResult commonRes = ResUtil.error(CommonResEnum.NO_HANDLER_FOUND);
            return RequestUtil.writeResponse(exchange.getResponse(), HttpStatus.NOT_FOUND, commonRes);
        }

        @Override
        public int getOrder() {
            return 30;
        }
    }
}
